import { Injectable } from '@angular/core';
import { Device } from "@ionic-native/device";
import { AliasOptions, JPush } from '@jiguang-ionic/jpush';
import { PushMsgModel } from '../../../model/services/native/push/push-msg-model';
import { ViewController } from 'ionic-angular';
import {WebcastDetailPage} from "../../../pages/webcast/webcast-detail/webcast-detail";

export abstract class PushService {

    protected static curAlias = "";

    public abstract init();

    public abstract setAlias(alias: string, successCallback?: ((value: String) => void), errorCallback?: ((error: any) => void));

    public abstract deleteAlias(alias: string, successCallback?: ((value: boolean) => void), errorCallback?: ((error: any) => void));

    public abstract getAlias(successCallback?: ((value: String) => void), errorCallback?: ((error: any) => void));

    public abstract openNotification(onOpenNotification: ((data: PushMsgModel) => void));

    public pushToOpenPage(msg: PushMsgModel, currentView: ViewController, navPushFunc: Function) {
        switch (msg.msgtype) {
            case 1:
                this.jumpScheduleDetail(msg, currentView, navPushFunc);
                break;
            case 3:
                this.jumpInformationDetail(msg, currentView, navPushFunc);
                break;
            case 4:
                this.jumpMessageDetail(msg, currentView, navPushFunc);
                break;
            case 5:
                this.jumpWebcastDetail(msg, currentView, navPushFunc);
                break;
            default:
                break;
        }
    }

    public setPushAlias() {
        let userCode = localStorage.getItem('userName');
        if (userCode == PushService.curAlias) {
           // console.log("setPushAlias delay")
            setTimeout(() => this.setPushAlias(), 10000);
            return;
        }

        if (userCode) {
            this.setAlias(userCode.trim().replace(" ","_"),
                (alias) => {
                  //  console.log("setPushAlias Success");
                    PushService.curAlias = userCode;
                    setTimeout(() => this.setPushAlias(), 10000);
                },
                (error) => {
              //      console.log("setPushAlias Error:" + JSON.stringify(error));
                    setTimeout(() => this.setPushAlias(), 5000);
                });
        } else {
            console.log("setPushAlias Error: No UserCode");
            setTimeout(() => this.setPushAlias(), 10000);
        }

    }

    public deletePushAlias() {
        let userCode = localStorage.getItem('userName');
        if (PushService.curAlias == "" || userCode == PushService.curAlias) {
           // console.log("deletePushAlias delay")
            setTimeout(() => this.deletePushAlias(), 10000);
            return;
        }

        this.deleteAlias(PushService.curAlias.trim().replace(" ","_"),
            (alias) => {
             //   console.log("deletePushAlias Success");
                PushService.curAlias = "";
                setTimeout(() => this.deletePushAlias(), 10000);
            },
            (error) => {
             //   console.log("deletePushAlias Error:" + error)
                setTimeout(() => this.deletePushAlias(), 5000);
            });
    }

    private jumpScheduleDetail(msg: PushMsgModel, currentView: ViewController, navPushFunc: Function) {
        const viewName = "ScheduleAddPage";
        let id = msg.extras.groupId;
        let date = msg.extras.scheduledate;

        if (currentView.id == viewName && currentView.instance.data != null && currentView.instance.data.id == id) {
            return;
        }
        navPushFunc.call(this, viewName, { id: id, date: date });
    }

    private jumpInformationDetail(msg: PushMsgModel, currentView: ViewController, navPushFunc: Function) {
        const viewName = "InformationDetailPage";
        let id = msg.extras.contentid;
        if (currentView.id == viewName && currentView.instance.infoDetail != null && currentView.instance.infoDetail.id == id) {
            return;
        }
        navPushFunc.call(this, viewName, { id: id });
    }

    private jumpMessageDetail(msg: PushMsgModel, currentView: ViewController, navPushFunc: Function) {
        const viewName = "MessageDetailPage";
        let id = msg.extras.messageid;
        if (currentView.id == viewName && currentView.instance.detailData != null && currentView.instance.detailData.id == id) {
            return;
        }
        navPushFunc.call(this, viewName, { id: id });
    }

    private jumpWebcastDetail(msg: PushMsgModel, currentView: ViewController, navPushFunc: Function) {
        const viewName = "WebcastDetailPage";
        console.log('jumpWebcastDetail', msg);
        let id = msg.extras.webcastid;
        if (currentView.id == viewName && currentView.instance.detailData != null && currentView.instance.detailData.id == id) {
            return;
        }
        navPushFunc.call(this, viewName, { id: id });
    }
}
